<?php
/**
 * ImpSoft Application Controller
 * This is the controller that all the other controllers extend. So any
 * functionality that needs to be in all of them should be in here or in a
 * controller plug-in or helper or something
 * 
 * @package ImpSoft
 * @subpackage ImpSoft Controllers
 * @copyright Luke Visinoni (luke.visinoni@gmail.com)
 * @author Luke Visinoni (luke.visinoni@gmail.com)
 * @license GNU Lesser General Public License 
 */
class ImpSoft_Controller_Action extends Zend_Controller_Action {

	protected $bootstrap;
	
	public function init() {
	
		/**
		 * The config object may be able to be grabbed from the bootstrap, so look into it
		 */
		$config = new Zend_Config_Ini(APPLICATION_PATH . '/config/app.ini', APPLICATION_ENV);
		$this->config = $config;
		
		/**
		 * Many of the resources we need throughout the application can be
		 * found within the bootstrap. In order to make it easier to access
		 * these resources, I will create shortcuts to them here.
		 */
		$front = Zend_Controller_Front::getInstance();
		$this->bootstrap = $front->getParam('bootstrap');
		
		// database adapter
		$this->db = $this->bootstrap->getResource('Db');
		// session facade
		$this->session = $this->bootstrap->getResource('Session');
		
		/**
		 * For every request, we need to check to see if a cart has been initialized.
		 * If no cart has been initialized, we need to initialize it. The cart has its
		 * own Zend_Session_Namespace which stores all of the data that is in the cart.
		 * @todo I need to find an elegant way to determine if a cart has already been
		 * initialized, and if so, to get access to it.
		 */
		$auth = Zend_Auth::getInstance();
		if ($user = $auth->getIdentity()) {
			// initialize cart!
			
		}
	
	}
	/**
	 * Get at the bootstrap object
	 */
	protected function getBootstrap() {
	
		return $this->bootstrap;
	
	}
    
    /**
     * post-dispatch logic - I use it here to add the flash messenger to the view
     */
    public function postDispatch() {
    
		$this->view->flash = $this->_helper->flashMessenger;
    
    }

	/**
	 * Pre-dispatch logic - I use it here to add the "user" session variable to the view
	 * so it will always be available in the view if the user is logged in. If they aren't
	 * logged in, it will be a null variable.
	 * 
	 * @todo It may make sense to eventually put the user logic into a plugin or something.
	 * @todo The category tree will definitely be a plugin/extension eventually
	 */
	public function preDispatch() {
	
		$auth = Zend_Auth::getInstance();
		$this->view->authid = $auth->getIdentity();
		// Supply the view with the category object so it can display the category tree
		$this->view->catTbl = new Category;
		
		/**
		 * @todo This needs to be in a plugin, not here. And pages.php should probably be changed to navigation.php
		 */
		$module = $this->getRequest()->getParam('module');
		switch ($module) {
			case "default":
			case "":
				/**
				 * @todo Until we have the extension system in place, I have to build out all of the navigation here
				 */
				
				break;
			default:
				$container = new Zend_Navigation(require APPLICATION_PATH . "/" . $module . "/pages.php");
				$this->view->navigation($container);
				break;
		}
	
	}
	
    /** Shortcuts to common helpers / actions below **/
    
    /**
     * shortcut to $this->_helper->flashMessenger->addMessage();
     */
    protected function flash($message, $namespace = null) {
    
        $this->_helper->flashMessenger($message, $namespace);
    
    }
    /**
     * shortcut to $this->getRequest()->isPost()
     */
    protected function isPost() {
    
        return $this->getRequest()->isPost();
    
    }
    /**
     * shortcut to $this->getRequest()->getPost()
     */
    protected function getPost() {
    
        return $this->getRequest()->getPost();
    
    }
    /**
     * 
     */
    protected function dump($message) {
    
        $this->_helper->debug->dump($message);
    
    }
    /**
     * 
     */
    protected function log($message) {
    
        $this->_helper->debug($message);
    
    }
    /**
     * shortcut to $this->_helper->redirector->gotoRoute();
     */
    protected function goToRoute($name, $urlOptions = array(), $reset = false) {
    
        $this->_helper->redirector->gotoRoute($urlOptions, $name, $reset);
    
    }
    /**
     * @todo - there is probably a better place for this than here... like an action helper
     */
    protected function isLoggedIn() {
    
        return Zend_Auth::getInstance()->hasIdentity();
    
    }
    /**
     * This is a shortcut to throw an exception that tells the dispatch loop to exit and issue a 404 not found page
     * @todo - if you pass a message to this function, it will be used as a flash message on the 404 page. You can also
     * pass a view to render so that you can show the user at least a relevant 404 page - "not found, but were you looking for...?"
     */
    protected function notFound($msg = null) {
    
        $exception = new ImpSoft_Controller_Exception_NotFound($msg);
        // $this->_flash($msg);
        throw $exception;
    
    }
    /**
     * Shortcut to set no render (disable auto-rendering of a view)
     */
    protected function disableViewRenderer() {
    
        $this->_helper->viewRenderer->setNoRender(true);
    
    }
	/**
	 * The cart is stored in the database. This will access the cart that is
	 * currently stored in the database with the current session id. We store
	 * a cached version of the cart in in the session.
	 * If it is available there, grab it. Otherwise, get it from
	 * the database. If it isn't available there, it needs to be initialized.
	 */
	protected function getCart() {
	
		$cart = array();
		try {
			// get cart from session
		} catch (ImpSoft_Exception_CartNotFound $e) {
			// get the cart from database
		} catch (Zend_Db_Exception $e) {
			// create cart and store in the session
		}
		return $cart;
	
	}

}